package pl.com.like.sequoia.web.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import pl.com.like.sequoia.biojava.util.IConnectionHelper;
import pl.com.like.sequoia.exceptions.LogicalException;
import pl.com.like.sequoia.model.bio.BioObjectAlign;
import pl.com.like.sequoia.model.bio.BioObjectHandler;
import pl.com.like.sequoia.model.bio.enums.FileTypeEnum;
import pl.com.like.sequoia.web.service.utils.SearchUtils;
import pl.com.like.sequoia.web.vo.SearchType;

/**
 * 
 * @author Pawel Wolanski
 * 
 */
public class SearchService implements ISearchService {

	private final static Logger log = LoggerFactory.getLogger(SearchService.class);

	@Autowired
	private IConnectionHelper connectionHelper;

	private FileTypeEnum convertType(SearchType type) {
		FileTypeEnum resultType;
		switch (type) {
			// case FASTA:
			// resultType = FileTypeEnum.FASTA;
			// break;
			// case GENBANK:
			// resultType = FileTypeEnum.GENBANK;
			// break;
			case PDB:
				resultType = FileTypeEnum.PDB_ZIP;
				break;
			default:
				resultType = FileTypeEnum.GENBANK;
		}
		return resultType;
	}

	public BioObjectHandler findBioObject(String idObject, SearchType type) throws LogicalException {
		FileTypeEnum fileType = convertType(type);
		log.info("trying to get object id: {}, with type {} ", new Object[] { idObject, fileType });
		return connectionHelper.getObject(idObject, fileType);
	}

	public List<BioObjectAlign> findBioObjectAlignList(String idObject, SearchType type) throws LogicalException {
		FileTypeEnum fileType = convertType(type);
		return connectionHelper.getSearchAlignList(idObject, fileType);
	}

	public List<BioObjectHandler> findBioObjectList(String idObject, SearchType type, Integer pageSize, Integer page)
			throws LogicalException {
		FileTypeEnum fileType = convertType(type);
		String inputValue = SearchUtils.getInstance().toSQLwildcards(idObject);
		log.info("trying to get list of objects id: {}, with type {} ", new Object[] { idObject, fileType });
		Boolean extendedSearch = Boolean.TRUE;
		return connectionHelper.getSearchList(inputValue, fileType, pageSize, extendedSearch, page);
	}

	public BioObjectAlign getAlign(String objectId) {
		return connectionHelper.getSearchAlign(objectId);
	}
}
